import { Component, OnInit } from '@angular/core';
import {SnackService} from '../../../service/snack.service';
import {MatSnackBar} from '@angular/material';
import {CompanyService} from '../../../service/company.service';
import {SnackSuccessComponent} from '../../snack-success/snack-success.component';
import {SnackErrorComponent} from '../../snack-error/snack-error.component';

@Component({
  selector: 'app-application-process',
  templateUrl: './application-process.component.html',
  styleUrls: ['./application-process.component.css', '../application-results/application-results.component.css']
})
export class ApplicationProcessComponent implements OnInit {
  appList: any = new Array();
  appAllList: any;
  status = 0;
  constructor(
    public companyService: CompanyService,
    public snackBar: MatSnackBar,
    public snackService: SnackService,
  ) {
    this.loadData();
  }

  ngOnInit() {
  }

  loadData() {
    this.companyService.appList()
      .subscribe(list => {
        this.appList = list;
        this.appAllList = list;
      });
  }
  appProcess(process: string, appId: number) {
    this.companyService.applicationProcessing(process, appId)
      .subscribe((res: any) => {
        if (res == 'OK') {
          this.snackService.message = '处理成功';
          this.snackBar.openFromComponent(SnackSuccessComponent, {
            duration: 3000,
          });
          this.loadData();
          // this.returnLogin();
        } else {
        }
      }, err => {
        console.error('ERROR', err);
        this.snackService.message = err.error.message;
        this.snackBar.openFromComponent(SnackErrorComponent, {
          duration: 3000,
        });
      });
  }
  statusFlag(status: number) {
    if (status != 0 ) {
      let newList = new Array();
      this.appAllList.forEach(function (e) {
        if (e.status == status) {
          newList.push(e);
        }
      })
      this.appList = newList;
    } else {
      this.appList = this.appAllList;
    }
    this.status = status;
  }
}
